#include "Devices.h"
#include "AXP202.h"

void Power_Init()
{
    /* Init PMU */
    Wire.begin(10, 9);  //SDA,SCL
    pmu.begin(&Wire);

    /* Enable and set LDO2 voltage */
    pmu.setOutputEnable(OP_LDO2, true);     //LDO2设置为输出
    pmu.setOutputVoltage(OP_LDO2, 3200);    //LDO2电压设置为3.000V

    /* Enable and set LDO3 voltage */
    pmu.setOutputEnable(OP_LDO3, true);     //LDO3设置为输出
    pmu.setOutputVoltage(OP_LDO3, 3300);    //LDO3电压设置为3.300V

    /* Enable and set LDO4 voltage */
    pmu.setOutputEnable(OP_LDO4, true);     //LDO4设置为输出
    pmu.setOutputVoltage(OP_LDO4, 3400);    //LDO4电压设置为3.300V

    /* Enable and set DCDC1 voltage */
    pmu.setOutputEnable(OP_DCDC2, true);    //DCDC1设置为输出
    pmu.setOutputVoltage(OP_DCDC2, 2200);   //DCDC1电压设置为3.300V

    /* Enable and set DCDC2 voltage */
    pmu.setOutputEnable(OP_DCDC3, true);    //DCDC2设置为输出
    pmu.setOutputVoltage(OP_DCDC3, 3300);   //DCDC2电压设置为2.275V

    /* Enable Battery Charging */
    pmu.setChargeEnable(true);              //充电功能使能
    pmu.setChargeCurrent(CHG_300mA);        //设置充电电流为300mA

    pmu.initKeyPressIRQ(LPRESS_2S);           //初始化按键中断事件 （设置长按键触发时间）

    /* Set off time */
    pmu.setPowerOffTime(POWEROFF_4S);       //设置关机后所有电源通道关闭时长为4S

    /* Set on time */
    pmu.setPowerOnTime(POWERON_128mS);      //设置开机后电源通道启动输出时长为128mS

    /* Enable VBUS ADC */
    pmu.setADCEnable(ADC_VBUS_V, true);     //VBUS ADC 电压使能
    pmu.setADCEnable(ADC_VBUS_C, true);     //VBUS ADC 电流使能

    /* Enable Battery ADC */
    pmu.setADCEnable(ADC_BAT_V, true);      //Battery ADC 电压使能
    pmu.setADCEnable(ADC_BAT_C, true);      //Battery ADC 电流使能
    
    Serial.println("AXP202 Set OK");
    Serial.println("AXP202 Open Success");
    
}

void KeyPressIRQEvent()
{
    if (pmu.powerState())
    {
        if (pmu.getShortPressIRQState())
        {
            Serial.println("PEK Short Press");
            pmu.setShortPressIRQDisabale();     //对应位写1结束中断
            static bool LOCK_STATE = 1; //初始化屏幕状态
            if (LOCK_STATE)
            {
                LOCK_STATE = 0;
            }
            else
            {
                LOCK_STATE = 1;
            } 
            /* 息屏时设置lightsleep或者deepsleep模式 */
        }
        else if (pmu.getLongPressIRQState())
        {
            Serial.println("PEK Long Press");
            pmu.setLongPressIRQDisabale();      //对应位写1结束中断
            Serial.println("Shut Down");
            pmu.powerOFF();                     //长按 2 秒关机
            /* 关机设置deepsleep模式待机或者直接关机 */
        }
    }
    else
    {
        Serial.println("AXP173 ERROR!");
        delay(1000); 
    }
}

void printPmuInfo()
{ 
    /* Get PMU temp info */
    Serial.printf("CoreTemp : ");
    Serial.println(pmu.getAXP202Temp());

    /* Get VBUS info */
    Serial.printf("VBUS_voltage : ");
    Serial.println(pmu.getVBUSVoltage());
    Serial.printf("VBUS_current : ");
    Serial.println(pmu.getVBUSCurrent());

    if(pmu.isBatExist())
    {
        Serial.println("Battery :Battery Exist");

        /* Get Battery info */
        pmu.isBatAct() ? Serial.println("Bat in activation mode") : Serial.println("Bat is not in activation mode");
        pmu.isCharging() ? Serial.println("Charging :Is Charging s") : Serial.println("Charging :NO or END Charging");  //充电状态
        Serial.printf("Bat_voltage : ");
        Serial.println(pmu.getBatVoltage());
        Serial.printf("Bat_Current : ");
        Serial.println(pmu.getBatCurrent());
        Serial.printf("Bat_BatPower : ");
        Serial.println(pmu.getBatPower());
    }
    else
    {
        Serial.println("Battery :NO Battery");
    }

}

void Power_Update()
{
    printPmuInfo();
    KeyPressIRQEvent();                         //按键中断事件判断
    delay(1000);
}